package com.aidex.biz.controller;

import com.aidex.biz.domain.AppVersion;
import com.aidex.biz.service.AppVersionService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * APP管理Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-08-16
 */
@RestController
@RequestMapping("/biz/bizAppVersion")
public class AppVersionController extends BaseController
{
    @Autowired
    private AppVersionService appVersionService;

    /**
     * 查询APP管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppVersion:list')")
    @GetMapping("/list")
    public R<PageInfo> list(AppVersion appVersion, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                appVersion.setCompanyId(null);
        }else{
                     appVersion.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        appVersion.setPage(new PageDomain(request, response));
        return R.data(appVersionService.findPage(appVersion));
    }

    /**
     * 获取APP管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppVersion:query')")
    @GetMapping(value = "/{id}")
    public R<AppVersion> detail(@PathVariable("id") String id)
    {
        return R.data(appVersionService.get(id));
    }

    /**
     * 新增APP管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppVersion:add')")
    @Log(title = "APP管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  AppVersion appVersion)
    {
        return R.status(appVersionService.save(appVersion));
    }

    /**
     * 修改APP管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppVersion:edit')")
    @Log(title = "APP管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated AppVersion appVersion)
    {
        return R.status(appVersionService.save(appVersion));
    }


    /**
     * 删除APP管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppVersion:remove')")
    @Log(title = "APP管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(appVersionService.deleteAppVersionByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppVersion:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(appVersionService.findMaxSort(new AppVersion()));
    }

    /**
     * 导出APP管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppVersion:export')")
    @Log(title = "APP管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(AppVersion appVersion)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    appVersion.setCompanyId(null);
            }else{
                         appVersion.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<AppVersion> list = appVersionService.findList(appVersion);
        ExcelUtil<AppVersion> util = new ExcelUtil<AppVersion>(AppVersion.class);
        return util.exportExcel(list, "APP管理数据");
    }

}
